package com.pyj.topline;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.pyj.topline.adapter.MyFragmentPagerAdapter;
import com.pyj.topline.fragment.CountFragment;
import com.pyj.topline.fragment.HomeFragment;
import com.pyj.topline.fragment.MeFragment;
import com.pyj.topline.fragment.VideoFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private RadioGroup radioGroup;

    private TextView tv_main_title;
    private RelativeLayout rl_title_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {

        viewPager = findViewById(R.id.viewPager);
        if (viewPager == null) {
            throw new RuntimeException("ViewPager is not found in the layout. Please check your XML file.");
        }
        //点击底部导航栏的首页按钮，出现首页界面
        HomeFragment homeFragment = new HomeFragment();
        CountFragment countFragment = new CountFragment();
        VideoFragment videoFragment = new VideoFragment();
        MeFragment meFragment = new MeFragment();
        List<Fragment> alFragment = new ArrayList<>();
        alFragment.add(homeFragment);
        alFragment.add(countFragment);
        alFragment.add(videoFragment);
        alFragment.add(meFragment);
        // ViewPager 设置适配器
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), alFragment));
        viewPager.setCurrentItem(0);


        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText("首页");
        rl_title_bar = findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(getResources().getColor(R.color.rdTextColorPress));
        radioGroup = findViewById(R.id.radioGroup);
        // RadioGroup选中状态改变监听
        radioGroup.setOnCheckedChangeListener((group, i) -> {
            // setCurrentItem方法中第二个参数控制页面切换动画，true：打开，false：关闭
            if (i == R.id.rb_home) {
                viewPager.setCurrentItem(0, false);
                Log.d("MainActivity", "主页触发");
            } else if (i == R.id.rb_count) {
                viewPager.setCurrentItem(1, false);
                Log.d("MainActivity", "统计触发");
            } else if (i == R.id.rb_video) {
                viewPager.setCurrentItem(2, false);
                Log.d("MainActivity", "视频触发");
            } else if (i == R.id.rb_me) {
                viewPager.setCurrentItem(3, false);
                Log.d("MainActivity", "我触发");
            }
        });

        radioGroup.check(R.id.rb_home); // 默认选中首页

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // 此方法通常用于页面滚动时的处理，但此处未实现具体逻辑 // 不需要处理
            }

            @Override
            public void onPageSelected(int position) {
                Log.d("MainActivity", "被调用的是 " + position);
                if (position == 0) {
                    radioGroup.check(R.id.rb_home);
                    tv_main_title.setText("首页");
                    rl_title_bar.setVisibility(View.VISIBLE);
                } else if (position == 1) {
                    radioGroup.check(R.id.rb_count);
                    tv_main_title.setText("统计");
                    rl_title_bar.setVisibility(View.VISIBLE);
                } else if (position == 2) {
                    radioGroup.check(R.id.rb_video);
                    tv_main_title.setText("视频");
                    rl_title_bar.setVisibility(View.VISIBLE);
                } else if (position == 3) {
                    radioGroup.check(R.id.rb_me);
                    tv_main_title.setText("我的");
                    rl_title_bar.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                // 方法体为空，可能是未实现具体逻辑
            }
        });
    }

    protected long exitTime; // 记录第一次点击时的时间

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(MainActivity.this, "再按一次退出黑马头条", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                MainActivity.this.finish();
                System.exit(0);
            }
            return true; // 表示事件已被消费，不再继续传递
        }
        return super.onKeyDown(keyCode, event); // 其他按键情况交给父类处理
    }
}